import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";

/**
 * @date 2018-06-25
 */
import { stringify } from 'qs';
import { getResponse, isTenantRoleLevel } from 'utils/utils';
import request from 'utils/request';
import { CPAAS_PLATFORM } from 'utils/config';
import { queryIdpValue } from 'cpaas-front/lib/services/api';
export var SERVICE_URL = "".concat(CPAAS_PLATFORM, "/v1/").concat(isTenantRoleLevel(), "banks");
export function serviceUrl() {
  var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  return "".concat(CPAAS_PLATFORM, "/v1/").concat(isTenantRoleLevel() ? "".concat(params.tenantId, "/") : '', "banks");
}
export var service = {
  init: function init() {
    return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee() {
      return _regeneratorRuntime.wrap(function _callee$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              return _context.abrupt("return", queryIdpValue('CPFM.BANK_TYPE'));

            case 1:
            case "end":
              return _context.stop();
          }
        }
      }, _callee);
    }))();
  },
  queryBanks: function queryBanks() {
    var _arguments = arguments;
    return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2() {
      var params, _params$page, page, _params$sort, sort, body;

      return _regeneratorRuntime.wrap(function _callee2$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              params = _arguments.length > 0 && _arguments[0] !== undefined ? _arguments[0] : {};
              _params$page = params.page, page = _params$page === void 0 ? {
                current: 1,
                pageSize: 10
              } : _params$page, _params$sort = params.sort, sort = _params$sort === void 0 ? {
                name: 'bankCode',
                order: 'asc'
              } : _params$sort, body = params.body;
              return _context2.abrupt("return", request("".concat(serviceUrl(params), "?page=").concat(page.current - 1, "&size=").concat(page.pageSize, "&sort=").concat(sort.name, ",").concat(sort.order, "&").concat(stringify(body))));

            case 3:
            case "end":
              return _context2.stop();
          }
        }
      }, _callee2);
    }))();
  },
  updateBank: function updateBank() {
    var _arguments2 = arguments;
    return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
      var params;
      return _regeneratorRuntime.wrap(function _callee3$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              params = _arguments2.length > 0 && _arguments2[0] !== undefined ? _arguments2[0] : {};
              return _context3.abrupt("return", request(serviceUrl(params), {
                method: params.bankId ? 'PUT' : 'POST',
                body: params
              }));

            case 2:
            case "end":
              return _context3.stop();
          }
        }
      }, _callee3);
    }))();
  },
  remove: function remove(params) {
    return _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee4() {
      return _regeneratorRuntime.wrap(function _callee4$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              return _context4.abrupt("return", request(serviceUrl(params), {
                method: 'DELETE',
                body: params
              }));

            case 1:
            case "end":
              return _context4.stop();
          }
        }
      }, _callee4);
    }))();
  }
};
export default {
  namespace: 'bank',
  state: {
    modalVisible: false,
    bankTypeList: [],
    list: {}
  },
  effects: {
    init: /*#__PURE__*/_regeneratorRuntime.mark(function init(_ref, _ref2) {
      var payload, call, put, response, data;
      return _regeneratorRuntime.wrap(function init$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref.payload;
              call = _ref2.call, put = _ref2.put;
              _context5.next = 4;
              return call(service.init, payload);

            case 4:
              response = _context5.sent;
              data = getResponse(response);

              if (!data) {
                _context5.next = 9;
                break;
              }

              _context5.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  bankTypeList: data
                }
              });

            case 9:
            case "end":
              return _context5.stop();
          }
        }
      }, init);
    }),
    fetch: /*#__PURE__*/_regeneratorRuntime.mark(function fetch(_ref3, _ref4) {
      var payload, call, put, response, list;
      return _regeneratorRuntime.wrap(function fetch$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref3.payload;
              call = _ref4.call, put = _ref4.put;
              _context6.next = 4;
              return call(service.queryBanks, payload);

            case 4:
              response = _context6.sent;
              list = getResponse(response);

              if (!list) {
                _context6.next = 9;
                break;
              }

              _context6.next = 9;
              return put({
                type: 'updateState',
                payload: {
                  list: list
                }
              });

            case 9:
            case "end":
              return _context6.stop();
          }
        }
      }, fetch);
    }),
    action: /*#__PURE__*/_regeneratorRuntime.mark(function action(_ref5, _ref6) {
      var method, payload, call, response;
      return _regeneratorRuntime.wrap(function action$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              method = _ref5.method, payload = _ref5.payload;
              call = _ref6.call;
              _context7.next = 4;
              return call(service[method], payload);

            case 4:
              response = _context7.sent;
              return _context7.abrupt("return", getResponse(response));

            case 6:
            case "end":
              return _context7.stop();
          }
        }
      }, action);
    })
  },
  reducers: {
    updateState: function updateState(state, action) {
      return _objectSpread(_objectSpread({}, state), action.payload);
    }
  }
};